const Controller = require("egg").Controller;

class PermissionController extends Controller {
  // 获取菜单列表
  async getPermissionList() {
    try {
      const permissionList =
        await this.ctx.service.permission.getPermissionList();
      this.ctx.body = {
        code: 200,
        data: permissionList,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }

  async getSelfPermissionList(){
    const { ctx } = this;
    const { id: user_id } = ctx.decode;
    try {
      const result = await ctx.service.permission.getSelfPermissionList(user_id);
      ctx.body = {
        code: 200,
        data: result,
      };
    } catch (error) {
      ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  

  }

  //修改菜单
  async modifyPermission() {
    const { ctx } = this;

    try {
      const { id, name, path, component, title,sort } = ctx.request.body;
      let params = {
        id,
        name,
        path,
        component,
        title,
        sort
      };
      const result = await this.ctx.service.permission.modifyPermission(params);
      this.ctx.body = {
        code: 200,
        data: result,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }

  //删除菜单
  async deletePermission() {
    const { ctx } = this;
    try {
      const { id } = ctx.request.body;
      const permission = await this.ctx.service.permission.deletePermission(id);

      this.ctx.body = {
        code: 200,
        data: permission,
      };
    } catch (error) {
      this.ctx.body = {
        code: -1,
        data: null,
        message: error.message,
      };
    }
  }
}

module.exports = PermissionController;
